﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Exceptions;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.Constants;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class ProductPersist : IProductPersist
    {
        public List<Persist.BLL.Model.TProductBaseModel> GetProductBaseModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new TProductBaseDao();
            return dao.GetList(sqlWhere);
        }

        public TProductHeaderModel GetProductHeaderModel(string barCode)
        {
            var list = GetProductHeaderModels(string.Format(" ProductItemBarCode = '{0}'", barCode));
            if (list.Count == 0)
                return null;
            return list[0];
        }

        public List<Persist.BLL.Model.TProductDetailModel> GetProductDetailModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new TProductDetailDao();
            return dao.GetList(sqlWhere);
        }

        public List<Persist.BLL.Model.TProductHeaderModel> GetProductHeaderModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new TProductHeaderDao();
            return dao.GetList(sqlWhere);
        }

        public void SaveProductBaseModels(List<Persist.BLL.Model.TProductBaseModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductBaseDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        dao.InsertItem(model.CreateOrm());
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        dao.UpdateItem(model.CreateOrm());
                    }
                }
                foreach (var model in list)
                {
                    model.EditFlag = Enums.EditFlag.None;
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void SaveProductDetailModels(List<Persist.BLL.Model.TProductDetailModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductDetailDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        dao.InsertItem(model.CreateOrm());
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        dao.UpdateItem(model.CreateOrm());
                    }
                }
                foreach (var model in list)
                {
                    model.EditFlag = Enums.EditFlag.None;
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void SaveProductHeaderModels(List<Persist.BLL.Model.TProductHeaderModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductHeaderDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        dao.InsertItem(model.CreateOrm());
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        dao.UpdateItem(model.CreateOrm());
                      
                    }
                }
                foreach (var model in list)
                {
                    model.EditFlag = Enums.EditFlag.None;
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void DeleteProductBaseModels(List<Persist.BLL.Model.TProductBaseModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductBaseDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.Delete)
                    {
                        dao.DeleteItem(model.CreateOrm());
                    }
                    else
                    {
                        throw new AppException("删除的实体状态EditFlag必须为Delete");
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void DeleteProductDetailModels(List<Persist.BLL.Model.TProductDetailModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductDetailDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.Delete)
                    {
                        dao.DeleteItem(model.CreateOrm());
                    }
                    else
                    {
                        throw new AppException("删除的实体状态EditFlag必须为Delete");
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void DeleteProductHeaderModels(List<Persist.BLL.Model.TProductHeaderModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProductHeaderDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.Delete)
                    {
                        dao.DeleteItem(model.CreateOrm());
                    }
                    else
                    {
                        throw new AppException("删除的实体状态EditFlag必须为Delete");
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        public void InsertProductBaseModel(TProductBaseModel baseModel)
        {
            var dao = new TProductBaseDao();
            dao.InsertItem(baseModel.CreateOrm());
            baseModel.EditFlag = Enums.EditFlag.None;
        }

        public void SaveProductHeaderAndDetailModels(List<TProductHeaderModel> headerModels,
                                      List<TProductDetailModel> detailModels)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var daoHeader = new TProductHeaderDao() { Trans = trans };
            var daoDetail = new TProductDetailDao() { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in headerModels)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        daoHeader.InsertItem(model.CreateOrm());
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        daoHeader.UpdateItem(model.CreateOrm());
                    }
                }
                foreach (var model in headerModels)
                {
                    model.EditFlag = Enums.EditFlag.None;
                }
                foreach (var model in detailModels)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        daoDetail.InsertItem(model.CreateOrm());
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        daoDetail.UpdateItem(model.CreateOrm());
                    }
                }
                foreach (var model in detailModels)
                {
                    model.EditFlag = Enums.EditFlag.None;
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }


        public TProductHeaderModel GetProductHeaderModel(string barCode, string procedureNo)
        {
            var list = GetProductHeaderModels(string.Format(" ProductItemBarCode = '{0}' and ProcedureNo='{1}'", barCode, procedureNo));
            if (list.Count == 0)
                return null;
            return list[0];
        }


        public List<VComponentScanBaseModel> GetVComponentScanBaseModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new VComponentScanBaseDao();
            return dao.GetList(sqlWhere);
        }
    }
}
